//
//  SiteTypeView.m
//  EasyToo
//
//  Created by Jr Lan on 13-9-1.
//
//

#import "SiteTypeView.h"

@implementation SiteTypeView
{
    id<typeSiteViewDelegate> _sitedelegate;
    UIAlertView *_baseAlert;
}

- (id)initWithFrame:(CGRect)frame andDelegate:(id<typeSiteViewDelegate>)sitedelegate
{
    _sitedelegate = sitedelegate;
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        // 设置tableView的数据源
        self.dataSource = self;
        // 设置tableView的委托
        self.delegate = self;
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.allSiteInfoArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
    
    AllSiteInfo *siteInfo = [self.allSiteInfoArray objectAtIndex:indexPath.row];
    
    //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 80)]autorelease];
    NSURL *photourl = [NSURL URLWithString:siteInfo.sceneLogo];
    //url请求实在UI主线程中进行的
    UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage
    imgview.image = images;
    [cell.contentView addSubview:imgview];
    
    //加载名字
    UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 220, 40)]autorelease];
    namelbl.text = siteInfo.sceneName;
    namelbl.font = [UIFont systemFontOfSize:15];
    namelbl.adjustsFontSizeToFitWidth = YES;
    [cell.contentView addSubview:namelbl];
    
    //加载描述
    UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 50, 220, 40)]autorelease];
    desclbl.text = siteInfo.description;
    desclbl.font = [UIFont systemFontOfSize:10];
    desclbl.textColor = [UIColor lightGrayColor];
    desclbl.lineBreakMode = UILineBreakModeWordWrap;
    desclbl.numberOfLines = 0;
    [cell.contentView addSubview:desclbl];

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AllSiteInfo *siteInfo = [self.allSiteInfoArray objectAtIndex:indexPath.row];
    NSMutableArray *tickerArray = [[NSMutableArray alloc]init];
    for (int i = 0; i < [self.siteTicketInfoArray count]; i++) {
        SiteTicketInfo *ticketInfo = [self.siteTicketInfoArray objectAtIndex:i];
        if ([ticketInfo.sceneId isEqualToString:siteInfo.sceneId]) {
            [tickerArray  addObject:ticketInfo];
        }
    }
    
    if ([tickerArray count] == 0) {
        _baseAlert = [[UIAlertView alloc]  initWithTitle:@"票务紧张" message:@"本景点的暂无多余门票"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
        [_baseAlert show];
        [_baseAlert release];
    } else {
        [_sitedelegate clickTypeSiteView:siteInfo siteTicketInfo:tickerArray];
    }
    
    [tickerArray release];
}
@end
